package com.bbva.gptf.back.dominio;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooDbManaged(automaticallyDelete = true)
@RooEntity(versionField = "", table = "TGPT021_LIST_CONTRA", schema = "DBSGPT", finders = { "findTgpt021ListContrasByCdGpoDelta" })
public class Tgpt021ListContra {

	public static List<Tgpt021ListContra> obtieneListaContratoPorGpoDeltaYGestor(
			final BigDecimal cdGpoDelta, final BigDecimal cdGestor) {
		final String qlString = "FROM Tgpt021ListContra listaContrato WHERE listaContrato.cdUsuario.cdUsuario = :cdGestor AND listaContrato.cdGpoDelta.cdGrupoDelta = :cdGpoDelta";
		try {
			return entityManager()
					.createQuery(qlString, Tgpt021ListContra.class)
					.setParameter("cdGestor", cdGestor)
					.setParameter("cdGpoDelta", cdGpoDelta).getResultList();
		} catch (final Exception e) {
			return null;
		}
	}

	public static List<Tgpt021ListContra> obtieneListaContratos(
			final BigDecimal cdGestor) {
		final String qlString = "FROM Tgpt021ListContra listaContrato WHERE listaContrato.cdUsuario.cdUsuario = :cdGestor";
		try {
			return entityManager()
					.createQuery(qlString, Tgpt021ListContra.class)
					.setParameter("cdGestor", cdGestor).getResultList();
		} catch (final Exception e) {
			return null;
		}
	}

	public static boolean eliminarAllListas(List<BigDecimal> cdLista) {
		try {
			final String qlString = "DELETE FROM Tgpt021ListContra listaContrato WHERE listaContrato.cdLista IN (:cdLista)";
			final int result = entityManager().createQuery(qlString)
					.setParameter("cdLista", cdLista).executeUpdate();
			System.out.println(":::.Filas afectadas: " + result);
			return true;
		} catch (final Exception e) {
			System.out.println(":::. Error en borrado " + e);
			return false;
		}
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "sq_lista_contrato")
	@SequenceGenerator(name = "sq_lista_contrato", sequenceName = "SQ_LISTA_CONTRATO")
	@Column(name = "CD_LISTA")
	private BigDecimal cdLista;
}
